package cn.xmkeshe.controller;

import cn.xmkeshe.back.ILoggerServiceBack;
import cn.xmkeshe.model.Logger;
import cn.xmkeshe.utils.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//权限日志
@Controller
public class LoggerController {

  @Resource
  private ILoggerServiceBack iLogServiceBack;

  @RequestMapping("/pages/back/log/customerPre")
  @Log(operationName = "用户权限日志页面",operationType = "管理员")
  public String customerPre(){
    return "/pages/back/log/customer";
  }

  @RequestMapping("/pages/back/log/customer")
  @ResponseBody
  @Log(operationName = "用户权限日志详情",operationType = "系统")
  public Map<String, Object> customer(Logger log, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    String value = "";
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    try{
      value = request.getParameter("value");
    }catch (Exception e){}
    Map<String, Object> map = iLogServiceBack.customer(currentPage,lineSize);
//    System.out.println(map);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }

  @RequestMapping("/pages/back/log/driverPre")
  @Log(operationName = "司机权限日志页面",operationType = "管理员")
  public String driverPre(){
    return "/pages/back/log/driver";
  }

  @RequestMapping("/pages/back/log/driver")
  @ResponseBody
  @Log(operationName = "司机权限日志详情",operationType = "系统")
  public Map<String, Object> driver(Logger log, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iLogServiceBack.driver(currentPage,lineSize);
//    System.out.println(map);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }

  @RequestMapping("/pages/back/log/systemPre")
  @Log(operationName = "系统权限日志页面",operationType = "管理员")
  public String systemPre(){
    return "/pages/back/log/system";
  }

  @RequestMapping("/pages/back/log/system")
  @ResponseBody
  @Log(operationName = "系统权限日志详情",operationType = "系统")
  public Map<String, Object> system(Logger log, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iLogServiceBack.system(currentPage,lineSize);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }

  @RequestMapping("/pages/back/log/adminPre")
  @Log(operationName = "管理员权限日志页面",operationType = "管理员")
  public String adminPre(){
    return "/pages/back/log/admin";
  }

  @RequestMapping("/pages/back/log/admin")
  @ResponseBody
  @Log(operationName = "管理员权限日志详情",operationType = "系统")
  public Map<String, Object> admin(Logger log, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iLogServiceBack.admin(currentPage,lineSize);
//    System.out.println(map);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }

  @RequestMapping("/pages/back/logger/delete")
  @ResponseBody
  @Log(operationName = "删除权限日志详情",operationType = "管理员")
  public String delete(int id){
    String flag = "error";
    if (iLogServiceBack.delete(id)){
      return "success";
    }
    return flag;
  }

  @RequestMapping("/pages/back/log/delAll")
  @ResponseBody
  @Log(operationName = "批量删除权限日志详情",operationType = "管理员")
  public String delAll(String ids) {
    String flag = "success";
    String[] strs = ids.split(",");
    for (String str : strs) {
      iLogServiceBack.delete(Integer.parseInt(str));
    }
    return flag;
  }

  //模糊查询权限日志详情
  @RequestMapping("/pages/back/log/findByCreateDate")
  @ResponseBody
  @Log(operationName = "模糊查询权限日志",operationType = "管理员")
  public Map<String, Object> findByCreateDate(String createDate, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iLogServiceBack.findByCreateDate(currentPage,lineSize,"%" + createDate + "%");
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }

}
